package com.woniu.controller;

import com.alipay.api.AlipayApiException;
import com.woniu.been.entity.CourseCategories;
import com.woniu.been.entity.CourseChapters;
import com.woniu.been.entity.Courses;
import com.woniu.been.entity.Teachers;
import com.woniu.been.entity.vo.CourseTreeVO;
import com.woniu.service.CourseCategoriesService;
import com.woniu.service.CourseChaptersService;
import com.woniu.service.CoursesService;

import com.woniu.utils.ResponseResult;
import com.woniu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseCategoriesController {

    @Autowired
    CourseCategoriesService courseCategoriesService;
    
    @Autowired
    CourseChaptersService courseChaptersService;

    @Autowired
    CoursesService coursesService;

    // 获取课程大分类（parent_category_id为null的为课程大类）
    @PostMapping("getMainMenu") // course/getMainMenu
    public ResponseResult<List<CourseCategories>> getMainCategories(CourseCategories courseCategories){
        List<CourseCategories> courseCategoriesList = courseCategoriesService.getMainMenu(courseCategories);
        if(courseCategoriesList!=null){
            return ResponseResult.success(courseCategoriesList);
        }else{
            return ResponseResult.fail(500,"服务端错误");
        }
    }

    @PostMapping("getClazzCenter") // course/getMainMenu
    public ResponseResult<List<Courses>> getClazzCenter(){
        List<Courses> courseCategoris = courseCategoriesService.getClazzCenter();
        if(courseCategoris!=null){
            return ResponseResult.success(courseCategoris);
        }else{
            return ResponseResult.fail(500,"服务端错误");
        }
    }

    // 根据课程大类ID获取课程小类（parent_category_id等于课程大类category_id的为课程小类）
    @GetMapping("/{categoryId}/subcategories")
    public Result<List<CourseCategories>> getSubCategories(@PathVariable Integer categoryId) {
        try {
            List<CourseCategories> subCategories = courseCategoriesService.getSubCategoriesByParentId(categoryId);
            return Result.success(subCategories);
        } catch (Exception e) {
            return Result.fail("获取课程小类失败: " + e.getMessage());
        }
    }
    
    // 根据课程小类ID获取章节列表（通过course_category_relations表关联）
    @GetMapping("/subcategory/{categoryId}/chapters")
    public Result<List<CourseChapters>> getChaptersBySubCategory(@PathVariable Integer categoryId) {
        try {
            List<CourseChapters> chapters = courseChaptersService.getChaptersBySubCategoryId(categoryId);
            return Result.success(chapters);
        } catch (Exception e) {
            return Result.fail("获取章节列表失败: " + e.getMessage());
        }
    }
    
    // 根据章节ID获取具体课程列表（通过course_chapters_relations表关联）
    @GetMapping("/chapter/{chapterId}/courses")
    public Result<List<Courses>> getCoursesByChapter(@PathVariable Integer chapterId) {
        try {
            List<Courses> courses = coursesService.getCoursesByChapterId(chapterId);
            return Result.success(courses);
        } catch (Exception e) {
            return Result.fail("获取课程列表失败: " + e.getMessage());
        }
    }
    
    // 获取完整的课程树结构（课程大类 > 课程小类 > 章节 > 具体课程）
    @GetMapping("/tree")
    public Result<List<CourseTreeVO>> getCourseTree() {
        try {
            List<CourseTreeVO> courseTree = courseCategoriesService.getCompleteCourseTreeVO();
            return Result.success(courseTree);
        } catch (Exception e) {
            return Result.fail("获取课程树失败: " + e.getMessage());
        }
    }
    
    // 根据课程大类ID获取完整的课程结构
    @GetMapping("/{categoryId}/tree")
    public Result<CourseTreeVO> getCourseTreeByCategory(@PathVariable Integer categoryId) {
        try {
            CourseTreeVO courseTree = courseCategoriesService.getCourseTreeVOByCategoryId(categoryId);
            return Result.success(courseTree);
        } catch (Exception e) {
            return Result.fail("获取课程结构失败: " + e.getMessage());
        }
    }

    // 查询所有课程大类（parent_category_id为null）
    @GetMapping
    public Result<List<CourseCategories>> list() {
        return Result.success(courseCategoriesService.getAllTopLevelCategories());
    }

    // 新增课程大类
    @PostMapping
    public Result<?> add(@RequestBody CourseCategories category) {
        courseCategoriesService.addCategory(category);
        return Result.success("新增成功");
    }

    // 修改课程大类
    @PutMapping("/{id}")
    public Result<?> update(@PathVariable Integer id, @RequestBody CourseCategories category) {
        category.setCategoryId(id);
        courseCategoriesService.updateCategory(category);
        return Result.success("修改成功");
    }

    // 删除课程大类
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        courseCategoriesService.deleteCategory(id);
        return Result.success("删除成功");
    }

}
